﻿Public Class frmMoniteursMAJ

    Public pNo_ligne As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset


    Private Sub frmMoniteur_UPD_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        InitComboAgences(CB_Agences)
        ' charger l'AFFECTATION du MONITEUR
        AffectationtoScreen()

    End Sub

    Private Sub AffectationtoScreen()

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGet_MONITEURS_AFF(" & pNo_ligne & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_CODE.Text = rs.Fields("cd_mon").Value
            CB_Agences.SelectedIndex = rs.Fields("agence").Value - 1
            DT_DATED.Value = rs.Fields("dated").Value
            DT_DATEF.Value = rs.Fields("datef").Value
            TE_DMATIN.Text = rs.Fields("dmatin").Value
            TE_FMATIN.Text = rs.Fields("fmatin").Value
            TE_DSOIR.Text = rs.Fields("dsoir").Value
            TE_FSOIR.Text = rs.Fields("fsoir").Value
            TE_REPOS1.Checked = rs.Fields("repos1").Value
            TE_REPOS2.Checked = rs.Fields("repos2").Value
            TE_REPOS3.Checked = rs.Fields("repos3").Value
            TE_REPOS4.Checked = rs.Fields("repos4").Value
            TE_REPOS5.Checked = rs.Fields("repos5").Value
            TE_REPOS6.Checked = rs.Fields("repos6").Value
            TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
        End If
        rs.Close()

    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click

        If DateDiff(DateInterval.Day, DT_DATED.Value, DT_DATEF.Value) < 0 Then
            MsgBox("La date de fin ne peut pas être inférieure à la date de début", MsgBoxStyle.Critical)
            Exit Sub
        End If

        ' mise à jour 
        szSQL = "CALL spUpd_MONITEURS_AFF(" & pNo_ligne & "," & CB_Agences.SelectedIndex + 1 & ",'" & Format(DT_DATED.Value, "yyyy/MM/dd") & "','" & _
        Format(DT_DATEF.Value, "yyyy/MM/dd") & "'," & TE_DMATIN.Text & "," & TE_FMATIN.Text & "," & _
        TE_DSOIR.Text & "," & TE_FSOIR.Text & "," & CInt(TE_REPOS1.Checked) & "," & CInt(TE_REPOS2.Checked) & "," & _
        CInt(TE_REPOS3.Checked) & "," & CInt(TE_REPOS4.Checked) & "," & CInt(TE_REPOS5.Checked) & "," & CInt(TE_REPOS6.Checked) & ")"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            Me.Close()
        End If

    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        ' utilisateur autorisé à modifier ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("moniteurs_aff", "affectation du moniteur " & TE_CODE.Text, pNo_ligne)
        ' afficher les affectations du moniteur
        Me.Close()

    End Sub

    Private Sub BO_PLANNING_Click(sender As System.Object, e As System.EventArgs) Handles BO_PLANNING.Click
        ' afficher la semaine type
        frmMoniteursSEM.pCD_MON = TE_CODE.Text
        frmMoniteursSEM.ShowDialog()
    End Sub
End Class